package com.kotlin.demo.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.kotlin.demo.constant.Constants
import com.kotlin.demo.enums.WriterType
import com.kotlin.demo.helper.DialogHelper
import com.kotlin.demo.tools.StringsUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

/**
 * 编辑key 弹窗
 */
@Composable
internal fun editKeyUi() {
    val transMap: HashMap<String, String> = Json.decodeFromString(
        Constants.translateResultText.value.trim()
    )
    val languageEn = transMap["en"].toString()//英文翻译
    // 翻译的key
    val translateKeyEn = StringsUtils.getTranslateKey(languageEn)
    val translateKey by remember { mutableStateOf(translateKeyEn) }//翻译res key
    if (translateKey.trim() == ("")) {
        return
    }
    comEditDialog(translateKey, "请确认写入的key值", "写入") { newTranslateKey ->
        if (newTranslateKey.trim() == ("")) {
            DialogHelper.showTipsDialog("翻译key不能为空！")
            return@comEditDialog
        }
        DialogHelper.showLoadingDialog("正在写入中...请稍后！")
        Constants.mainScope.launch(Dispatchers.Default) {
            // 去写入翻译
            if (StringsUtils.isExistTranslateKey(Constants.resPath.value, newTranslateKey)) {//已存在相同多语言
                DialogHelper.dismissDialog()
                delay(200L)
                DialogHelper.showTipsDialog("已存在相同的多语言！") {
                    Constants.translateKey.value = newTranslateKey.trim()
                    DialogHelper.showReplaceKeyDialog()
                }
                return@launch
            }
            try {
                StringsUtils.startInsertLanguage(
                    newTranslateKey,
                    transMap,
                    Constants.resPath.value,
                    WriterType.INSERT
                )
                DialogHelper.dismissDialog()
                delay(100L)
                DialogHelper.showTipsDialog("写入翻译成功！")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}

/**
 * 替换多语言弹窗
 */
@Composable
internal fun replaceTranslateKeyView() {
    val data = Constants.translateResultText.value
    var transMap: HashMap<String, String> = HashMap()
    if (data.isNotEmpty()) {
        transMap = Json.decodeFromString(
            Constants.translateResultText.value
        )
    }
    var currentKey = Constants.translateKey.value.trim()
    if (currentKey.isEmpty() && Constants.translateResultText.value.isNotEmpty()) {
        currentKey = transMap["en"].toString()//英文翻译
        // 翻译的key
        currentKey = StringsUtils.getTranslateKey(currentKey)
    }
    comEditDialog(currentKey, "请确认要替换的key值", "替换") { newTranslateKey ->
        if (newTranslateKey.trim() == ("")) {
            DialogHelper.showTipsDialog("翻译key不能为空！")
            return@comEditDialog
        }
        Constants.mainScope.launch(Dispatchers.Default) {
            // 去翻译
            try {
                if (!StringsUtils.isExistTranslateKey(Constants.resPath.value, newTranslateKey)) {
                    DialogHelper.dismissDialog()
                    DialogHelper.showTipsDialog("替换的翻译不存在")
                    return@launch
                }
                DialogHelper.showLoadingDialog("正在替换中...请稍后！")
                // 替换相当于 先移除
                StringsUtils.removeTranslateByKey(Constants.resPath.value, newTranslateKey)
                //再写入
                StringsUtils.startInsertLanguage(newTranslateKey, transMap, Constants.resPath.value, WriterType.INSERT)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            DialogHelper.dismissDialog()
            DialogHelper.showTipsDialog("替换成功！")
        }
    }
}

/**
 * 移除多语言弹窗
 */
@Composable
internal fun removeTranslateKeyView() {
    comEditDialog("", "请确认要移除的key值", "移除") { removeTranslateKey ->
        if (removeTranslateKey.isEmpty()) {
            DialogHelper.showTipsDialog("要移除的key不能为空！")
            return@comEditDialog
        }
        DialogHelper.showLoadingDialog("正在移除中...请稍后！")
        Constants.mainScope.launch(Dispatchers.Default) {
            try {
                if (!StringsUtils.isExistTranslateKey(Constants.resPath.value, removeTranslateKey)) {
                    DialogHelper.dismissDialog()
                    DialogHelper.showTipsDialog("要移除的翻译不存在!")
                    return@launch
                }
                StringsUtils.removeTranslateByKey(Constants.resPath.value, removeTranslateKey)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            DialogHelper.dismissDialog()
            DialogHelper.showTipsDialog("移除成功！")
        }
    }
}

/**
 *预览多语言弹窗
 */
@Composable
internal fun findTranslateKeyView() {
    comEditDialog("", "请确认要预览的key值", "预览") { findTranslateKey ->
        if (findTranslateKey.isEmpty()) {
            DialogHelper.showTipsDialog("要预览的key不能为空！")
            return@comEditDialog
        }
        DialogHelper.showLoadingDialog("预览翻译查找中...")
        Constants.mainScope.launch(Dispatchers.Default) {
            try {
                if (!StringsUtils.isExistTranslateKey(Constants.resPath.value, findTranslateKey)) {
                    DialogHelper.dismissDialog()
                    DialogHelper.showTipsDialog("要预览的翻译不存在!")
                    return@launch
                }
                val resultMap = StringsUtils.getTranslateKeyLanguages(findTranslateKey, Constants.resPath.value)
                val json = Json { prettyPrint = true }
                Constants.translateResultText.value = json.encodeToString(resultMap)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            DialogHelper.dismissDialog()
            DialogHelper.showTipsDialog("预览成功！")
        }
    }
}

